from django import forms
from .models import ChatRoom
class CreateRoomForm(forms.ModelForm):
    class Meta:
        model = ChatRoom
        fields = ['name','description']
        widgets = {
            'name': forms.TextInput(attrs={'class': 'form-control','placeholder':'请输入聊天室名称'}),
            'description': forms.Textarea(attrs={'class': 'form-control',
                'placeholder':'请输入聊天室描述（可选）',
                 'rows':3
            }),
        }
    def clean__name(self):
        name = self.cleaned_data['name'].strip()
        # if ChatRoom.objects.contains(name=name): # 写法不对 contains必须是模型对象
        if not name:
            raise forms.ValidationError('请输入名称，不可为空')
        if ChatRoom.objects.filter(name=name).exists():
            raise forms.ValidationError('该聊天室名称已被使用')
        return name

# class ManageRoomForm(forms.models):
#     class Meta:
#         models = ChatRoom
#         fields = ['name','descriptions']
#         widgets = {
#             'name': forms.TextInput(attrs={'placeholder':'请输入聊天室名称'}),
#             'description': forms.Textarea(attrs={
#                 'placeholder':'请输入聊天室描述（可选）',
#                  'rows':3
#             }),
#         }
#         def clean__name(self):
#             name = self.cleaned_data['name'].strip()
#
